package org.brain.dbo;

import java.util.ArrayList;
import java.util.List;

import org.brain.pojos.Commentinfo;
import org.brain.pojos.Document;
import org.brain.pojos.Documenttype;
import org.brain.pojos.Messagetable;
import org.brain.pojos.Treemenu;
import org.brain.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;



public class DBOperate {
	static Session session = HibernateUtil.getSession();
	
	
	//获取对象的所有数据
	public static List search(String keyword){
		String sql = "from "+keyword+"";
		Query query = session.createQuery(sql);
		List list =query.list();
		if(list.size()==0)
			return new ArrayList();
		return list;
	}
	
	//根据用户名和父节点， 获取Treemenu
	public static List<Treemenu> getTreeMenu(String userName,int fatherNode)
	{
		List<Treemenu> list = new ArrayList();
		String sql = "from Treemenu where username=? and fathernode=?";
		Query query = session.createQuery(sql);
		query.setString(0, userName);
		query.setInteger(1, fatherNode);
		list = query.list();
		if(list.size()==0)
			list = new ArrayList();
		return list;
		
	}
	
	//根据nodeId号来获取TreeMenu的某个对象
	public static Treemenu getTreeMenu(int nodeId)
	{
		String sql ="from Treemenu where nodeId ="+nodeId;
		Query query = session.createQuery(sql);
		List list = query.list();
		return (Treemenu)list.get(0);
		
	}
	
	
	//根据Documenttyped的typeid获取documenttype对象
	public static Documenttype getDocumenttype(int typeId)
	{
		String sql ="from Documenttype where typeId ="+typeId;
		Query query = session.createQuery(sql);
		List list = query.list();
		return (Documenttype)list.get(0);
	}
	
	// 根据评论的编号。来返回一个评论对象
	public static Commentinfo getCommentinfo(int commentId)
	{
		String sql = "from Commentinfo where commentId =?";
		Query query = session.createQuery(sql);
		query.setInteger(0, commentId);
		List list = query.list();
		return (Commentinfo)list.get(0);
	}
	// 根据文档的编号。来返回一个文档对象
	public static Document getDocument(int documentId)
	{
		String sql = "from Document where documentId =?";
		Query query = session.createQuery(sql);
		query.setInteger(0, documentId);
		List list = query.list();
		return (Document)list.get(0);
	}
	// 根据消息的编号。来返回一个消息对象
	public static Messagetable getMessageobj(int messageId)
	{
		String sql = "from Messagetable where messageid =?";
		Query query = session.createQuery(sql);
		query.setInteger(0, messageId);
		List list = query.list();
		return (Messagetable)list.get(0);
	}
	//删除对象
	public static boolean del(Object object)
	{
		boolean flag = false;
		Transaction tr = session.beginTransaction();
		try{
		session.delete(object);
		tr.commit();
		flag = true;
		}catch(Exception e)
		{
			e.printStackTrace();
			tr.rollback();
		}
		return flag;
	}
	
	
	//删除菜单项的子节点
	public static boolean delChildNode(Treemenu treeMenu)
	{
		boolean flag = false;
		Transaction tx = null;
		try{
			treeMenu.getNodeid();
			List<Treemenu> list = search("Document where nodeid="+treeMenu.getNodeid());
			if(list!= null){
					delList(list);
			}
			tx = session.beginTransaction();
			session.delete(treeMenu);
			tx.commit();
			flag = true;
		}catch(Exception e)
		{
			e.printStackTrace();
			tx.rollback();
		}	
		return flag;
		
	}
	
	public static boolean delFatherNode(Treemenu treeMenu)
	{
		Transaction tx = null;
		try{
			treeMenu.getNodeid();
			List<Treemenu> list = search("Document where nodeid="+treeMenu.getNodeid());
			if(list!= null){
				for(Treemenu menu:list)
					delChildNode(menu);
			}
			tx = session.beginTransaction();
			session.delete(treeMenu);
			tx.commit();
			return true;
		}catch(Exception e)
		{
			e.printStackTrace();
			tx.rollback();
			return false;
		}	
		
		
	}
	
	public static boolean delList(List list)
	{
		Transaction tx = session.beginTransaction();
		try {
			for(int i=0;i<list.size();i++)
			{
				session.delete(list.get(i));
			}
			tx.commit();
			return true;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			tx.rollback();
			return false;
		}
	}
	
	//保存对象 ，返回一个boolean 来判断状态
	public static boolean save(Object object){
		Session sessions = HibernateUtil.getSession();
		Transaction transaction = sessions.beginTransaction();
		try {
			sessions.save(object);
			transaction.commit();
			return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			if(transaction!=null)
				transaction.rollback();
			e.printStackTrace();
		return false;
			
		}
	
	}
	//更新对象
	public static boolean update(Object object){
		Transaction transaction = session.beginTransaction();
		try {
			session.update(object);
			transaction.commit();
			return true;
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			transaction.rollback();
			return false;
			
		}
	}
}